import 'package:flutter/material.dart';

enum AnimationType {
  rightToLeft,  // 从右到左
  leftToRight,  // 从左到右
  bottomToTop,  // 从下到上
  topToBottom,  // 从上到下
  fade,         // 淡入淡出
  scale,        // 缩放
  rotation      // 旋转
}

class RouteAnimation {
  void animateRoute(BuildContext context, Widget page, {
    AnimationType type = AnimationType.rightToLeft,
    Duration duration = const Duration(milliseconds: 300),
    Curve curve = Curves.easeInOut,
  }) {
    Navigator.push(
      context,
      PageRouteBuilder(
        settings: RouteSettings(name: ''), // 保留路由名称
        pageBuilder: (_, animation, __) => page, // 替换为你的目标页面
        transitionsBuilder: (_, animation, __, child) {
          // 根据动画类型选择不同的动画效果
          switch (type) {
            case AnimationType.rightToLeft:
              return _buildSlideTransition(
                animation, 
                child, 
                const Offset(1.0, 0.0), 
                Offset.zero,
                curve
              );
            case AnimationType.leftToRight:
              return _buildSlideTransition(
                animation, 
                child, 
                const Offset(-1.0, 0.0), 
                Offset.zero,
                curve
              );
            case AnimationType.bottomToTop:
              return _buildSlideTransition(
                animation, 
                child, 
                const Offset(0.0, 1.0), 
                Offset.zero,
                curve
              );
            case AnimationType.topToBottom:
              return _buildSlideTransition(
                animation, 
                child, 
                const Offset(0.0, -1.0), 
                Offset.zero,
                curve
              );
            case AnimationType.fade:
              return FadeTransition(
                opacity: animation,
                child: child,
              );
            case AnimationType.scale:
              return ScaleTransition(
                scale: animation,
                child: child,
              );
            case AnimationType.rotation:
              return RotationTransition(
                turns: animation,
                child: child,
              );
          }
        },
        transitionDuration: duration, // 动画时长
      ),
    );
  }

  // 构建滑动过渡动画
  Widget _buildSlideTransition(
    Animation<double> animation, 
    Widget child, 
    Offset begin, 
    Offset end,
    Curve curve
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: begin,
        end: end,
      ).animate(CurvedAnimation(
        parent: animation,
        curve: curve,
      )),
      child: child,
    );
  }
}
